﻿#ifndef REGISTERCENTER_DEVICE_H
#define REGISTERCENTER_DEVICE_H

#include <qobject.h>
#include <qhash.h>
#include <qhostaddress.h>
#include "ObjAutoInject.h"

#include "LogSaver.h"

class Device;
class HeartBeatCodec;
class TurnTableCodec;
class RegisterCenter_Device : public QObject,public ObjAutoInject
{
  Q_OBJECT
private://property
  QHash<uint/*deviceId*/,Device*> deviceMap;
  const int defaultLifeTime=50*1000;//超时时间稍微大于真正的超时时间，避免网络延时等因素的干扰
  const int defaultHeartBeatTime=5*1000;//默认心跳间隔
  const int defaultCurrentStateTime=500;//默认检测当前状态间隔
  const int defaultRunningStateTime=1000;//默认检测运行状态间隔

private://dependency
  HeartBeatCodec* heartBeatCodec=Q_NULLPTR;
  TurnTableCodec* turnTableCodec=Q_NULLPTR;
  LogSaver* logSaver=LogSaver::getLogSaver();
public:
  explicit RegisterCenter_Device();
  virtual ~RegisterCenter_Device();
signals:
  void signal_deviceOffline(Device* device);
  void signal_deviceOnline(Device* device);
public:
  bool isOnline(uint deviceId);
  void updateLifeTime(uint deviceId, ushort msgId, uint ip=0, ushort port=0, int lifeTime_ms=0);
  Device* getDeviceById(uint deviceId);
public://login and logout
  void deviceLogin(Device* device);
  void deviceLogout(uint deviceId);
private slots:
  void slot_lifeTimeout(Device *device);
  void slot_heartBeatTimeout(Device *device);
  void slot_currentStateTimeout(Device *device);
  void slot_runningStateTimeout(Device *device);
public://geter and seter
  int getDefaultLifeTime();
  int getDefaultHeartBeat();
  HeartBeatCodec* getHeartBeatCodec()const;
  RegisterCenter_Device* setHeartBeatCodec(HeartBeatCodec*const heartBeatCodec);
  TurnTableCodec* getTurnTableCodec()const;
  RegisterCenter_Device* setTurnTableCodec(TurnTableCodec*const turnTableCodec);
public:
  virtual void objectAutoInjection(QHash<QString,QObject*>* objMap);
};
#endif // REGISTERCENTER_DEVICE_H
